package com.dg.cuckoo.admin.service.impl;

import com.dg.cuckoo.admin.dto.acceptance.AcceptanceDto;
import com.dg.cuckoo.admin.dto.basicInformation.projectInformation.BasicProjectDto;
import com.dg.cuckoo.admin.dto.earlyProject.EarlyProjectDto;
import com.dg.cuckoo.admin.dto.earlyProject.TwoEarlyProjectDto;
import com.dg.cuckoo.admin.dto.other.FileDto;
import com.dg.cuckoo.admin.repository.*;
import com.dg.cuckoo.admin.service.AcceptanceService;
import com.dg.cuckoo.common.utils.PageModel;
import com.dg.cuckoo.core.ServiceException;
import net.sf.json.JSONObject;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class AcceptanceServiceimpl implements AcceptanceService {

    @Autowired
    private AcceptanceRepository repository;
    @Autowired
    private TwoEarlyProjectRepository twoEarlyProjectRepository;
    @Autowired
    private BasicProjectRepository basicProjectRepository;
    @Autowired
    private FileRepository fileRepository;
    @Autowired
    private EarlyProjectRepository earlyProjectRepository;

    @Override
    public void save(AcceptanceDto dto) {
        checkSave(dto);
        repository.save(dto);
    }

    @Override
    public void delect(String id) {
        if (id == null) {
            throw new ServiceException("请选择行");
        }
        repository.deleteById(id);
    }

    @Override
    public PageModel page(Integer page, Integer pageSize) {
        Integer one = page;
        page = (page - 1) * pageSize;
        int size = repository.findAll().size();
        List<AcceptanceDto> dtos = repository.page(page, pageSize);
        return new PageModel(one, pageSize, size, dtos);
    }

    @Override
    public void update(String id, AcceptanceDto dto) {
        if (id == null) {
            throw new ServiceException("请选择行");
        }
        checkSave(dto);
        dto.setId(id);
        repository.save(dto);
    }

    @Override
    public String openInfo(String id) {
            if (id == null) {
            throw new ServiceException("请选择行");
        }

        BasicProjectDto basicProjectDto = basicProjectRepository.findByMainName(repository.findById(id).get().getProjectName());
        if (basicProjectDto == null) {
            throw new ServiceException("此项目已不存在，请删除");
        }
        String proId = basicProjectDto.getId();
        AcceptanceDto dto = repository.findById(id).get();
        JSONObject json = new JSONObject();
        json.put("dto", dto);

        List<FileDto> ky = new ArrayList<>();//可研报批
        ky.addAll(filesUtil(proId, 1));
        json.put("ky", ky);

        List<FileDto> gh = new ArrayList<>();//估算审核
        gh.addAll(earlyfilesUtil(proId, 10));
        json.put("gh", gh);

        List<FileDto> kf = new ArrayList<>();//可研批复
        kf.addAll(earlyfilesUtil(proId, 11));
        json.put("kf", kf);

        List<FileDto> cg = new ArrayList<>();//初步设计报告
        cg.addAll(filesUtil(proId, 2));
        json.put("cg", cg);

        List<FileDto> gssh = new ArrayList<>();//概算审核
        gssh.addAll(earlyfilesUtil(proId, 12));
        json.put("gssh", gssh);

        List<FileDto> cf = new ArrayList<>();//初步设计批复
        cf.addAll(earlyfilesUtil(proId, 13));
        json.put("cf", cf);

        List<FileDto> st = new ArrayList<>();//施工图
        st.addAll(filesUtil(proId, 3));
        json.put("st", st);

        List<FileDto> yh = new ArrayList<>();//预算审核
        yh.addAll(earlyfilesUtil(proId, 14));
        json.put("yh", yh);

        List<FileDto> ss = new ArrayList<>();//施工图联审
        ss.addAll(earlyfilesUtil(proId, 15));
        json.put("ss", ss);
        return json.toString();
    }

    public List<FileDto> filesUtil(String proId, Integer type) {
        List<TwoEarlyProjectDto> dtos = twoEarlyProjectRepository.findByProjectIdAndType(proId, type);
        List<FileDto> fileDtos = new ArrayList<>();
        if (fileDtos.size()>0){
            for (TwoEarlyProjectDto t : dtos) {
                List<FileDto> byByMark = fileRepository.findByByMark(t.getId());
                fileDtos.addAll(byByMark);
            }
        }
        return fileDtos;
    }

    public List<FileDto> earlyfilesUtil(String proId, Integer type) {
        List<EarlyProjectDto> dtos = earlyProjectRepository.findByProjectIdAndType(proId, type);
        List<FileDto> fileDtos = new ArrayList<>();
        if(fileDtos.size()>0){
            for (EarlyProjectDto t : dtos) {
                List<FileDto> byByMark = fileRepository.findByByMark(t.getId());
                fileDtos.addAll(byByMark);
            }
        }
        return fileDtos;
    }


    public void checkSave(AcceptanceDto dto) {
        if (dto.getProjectName() == null) {
            throw new ServiceException("请输入项目名称");
        }
        if (repository.findByProjectName(dto.getProjectName()) != null) {
            throw new ServiceException("项目名称已存在");
        }
    }
}
